<?php
class Summary extends MY_Controller
{
	public function __construct()
	{
		log_message('debug', '=====开始调用Currency->Summary->__construct接口=====');
		parent::__construct();
		$this->loadLanguageFile('application/controllers/Currency/');
		log_message('debug', '=====调用Currency->Summary->__construct接口结束=====');
	}
	
	public function showView()
	{
		$this->load->view('module/Currency/summary.htm');
	}
	
	public function getListData()
	{
		log_message('debug', '=====开始调用Currency->Summary->getListData接口=====');
		//error_log('debug=====开始调用Currency->Summary->getListData接口=====');
		$this->verifyPrivilege();
		
		try {
			$gameAreaIdString = $this->param->getParam('gameAreaIdString', array('method' => 'string', 'emptyErrorCode' => 201, 'invalidErrorCode' => 1));
			//error_log("gameAreaIdString = ".$gameAreaIdString);
			$currencyTypeId = $this->param->getParam('currencyTypeId', array('method' => 'int', 'min' => 1, 'max' => 999999, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1));
			//error_log("gameAreaIdString = ".$gameAreaIdString.", currencyTypeId = ".$currencyTypeId);
			$startDate = $this->param->getParam('startDate', array('method' => 'string', 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			//error_log("gameAreaIdString = ".$gameAreaIdString.", currencyTypeId = ".$currencyTypeId.", startDate = ".$startDate);
			$endDate = $this->param->getParam('endDate', array('method' => 'string', 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			//error_log("gameAreaIdString = ".$gameAreaIdString.", currencyTypeId = ".$currencyTypeId.", startDate = ".$startDate.", endDate = ".$endDate);
			$page = $this->param->getParam('page', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			//error_log("gameAreaIdString = ".$gameAreaIdString.", currencyTypeId = ".$currencyTypeId.", startDate = ".$startDate.", endDate = ".$endDate.", page = ".$pages);
			$rows = $this->param->getParam('rows', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			//error_log("gameAreaIdString = ".$gameAreaIdString.", currencyTypeId = ".$currencyTypeId.", startDate = ".$startDate.", endDate = ".$endDate.", page = ".$page.", $rows = ".$rows);
			
			$sql = "
				select
					<selectColumns>
				from
					`t_data_currency_summary` a
				left join
					`t_data_game_area` b
				on
					a.`gameAreaId` = b.`gameAreaId`
				left join
					`t_data_operator` c
				on
					b.`operatorId` = c.`operatorId`
				left join
					(
						select
							a.`functionId`,
							a.`function{$this->currLanguage}Name`,
							if(b.`subFunctionId` is null, 0, b.`subFunctionId`) as subFunctionId,
							b.`subFunction{$this->currLanguage}Name`
						from
							`t_cfg_currency_function` a
						left join
							`t_cfg_currency_subfunction` b
						on
							a.`functionId` = b.`functionId`
					) d
				on
					a.`functionId` = d.`functionId`
					and
					a.`subFunctionId` = d.`subFunctionId`
				where
					a.`currencyTypeId` = '{$currencyTypeId}'
					and
					a.`gameAreaId` in ({$gameAreaIdString})
			";
			
			
			if($startDate) {
				$sql .= " and a.`date` >= '{$startDate}'";
			}
			
			if($endDate) {
				$sql .= " and a.`date` <= '{$endDate}'";
			}
			
			$getTotalSql = str_replace('<selectColumns>', "count(*)", $sql);
			//error_log("sql 1 = ".$getTotalSql);
			$this->returnData['data']['total'] = $this->gmDatabase->getColumn($getTotalSql);
			$getDataSql = str_replace('<selectColumns>', "c.`operatorName`, b.`areaNum`, a.`date`, a.`functionId`, if(d.`function{$this->currLanguage}Name` is null, a.`functionId`, d.`function{$this->currLanguage}Name`) as `functionName`, if(d.`subFunction{$this->currLanguage}Name` is Null, a.`subFunctionId`, d.`subFunction{$this->currLanguage}Name`) as `subFunctionName`, a.`getValue`, a.`costValue`", $sql);
			$getDataSql .= " order by a.`date` asc, a.`costValue` desc";
			if($page && $rows) {
				$startRow = ($page - 1) * $rows;
				$getDataSql .= " limit {$startRow}, {$rows}";
			}
			//error_log("sql 2 = ".$getDataSql);
			$this->returnData['data']['rows'] = $this->gmDatabase->getArray($getDataSql);
		}
		
		catch(MY_Param_Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		catch(MY_Mysql_Exception $e) {
			$this->setErrorCode(1);
		}
		
		catch(Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		$this->returnJson();
		log_message('debug', '=====调用Currency->Summary->getListData接口结束=====');
	}
}